package com.cy.store.Service.Impl;

import com.cy.store.Entity.Cart;
import com.cy.store.Entity.Product;
import com.cy.store.Mapper.CartMapper;
import com.cy.store.Mapper.ProductMapper;
import com.cy.store.Service.Ex.AccessDeniedException;
import com.cy.store.Service.Ex.CartNotFoundException;
import com.cy.store.Service.Ex.InsertException;
import com.cy.store.Service.Ex.UpdateException;
import com.cy.store.Service.ICartService;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    /**购物车的业务层依赖于购物车的持久层以及商品的持久层*/
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        Date date = new Date();
        //根据参数pid和uid查询购物车中该商品是否已经存在
        if (result == null) {
            Cart cart = new Cart();

            //封装数据：uid,pid,amount
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);//注意前端传来amount时并没有和数据库商品数量进行求和

            //查询商品数据，得到商品价格并封装
            Product product = productMapper.findById(pid);
            cart.setPrice(product.getPrice());

            //封装数据：4个日志
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedUser(username);
            cart.setModifiedTime(date);

            Integer rows = cartMapper.insert(cart);
            if (rows != 1) {
                try {
                    throw new InsertException("插入数据时出现未知异常");
                } catch (InsertException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {//表示当前的商品清单在购物车已经存在，则更新这条是数据的num值
            //从查询结果中取出原数量，与参数amount相加，得到新的数量
            Integer num = null;
            if (result.getNum() == null) {
                num=amount;
            } else {
                num = result.getNum() + amount;
            }

            Integer rows = cartMapper.updateNumByCid(
                    result.getCid(),
                    num,
                    username,
                    date);
            if (rows != 1) {
                try {
                    throw new InsertException("更新数据时产生未知异常");
                } catch (InsertException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    @Override
    public List<CartVO> getVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }


    @Override
    public Integer addNum(Integer cid, Integer uid, String username) throws CartNotFoundException, AccessDeniedException, UpdateException {
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("数据不存在");
        }
        if (!result.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum() + 1;
        Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (rows != 1) {
            throw new UpdateException("更新数据时产生未知异常");
        }
        return num;
    }

    @Override
    public List<CartVO> getVOByCid(Integer uid, Integer[] cids) {
        List<CartVO> list=cartMapper.findVOByCid(cids);
        Iterator<CartVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            CartVO cartVO = iterator.next();
            if (!cartVO.getUid().equals(uid)) {
                //表示当前的数据不属于当前的用户
                list.remove(cartVO);
            }
        }
        return list;
    }

}
